<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/http.js"></script>
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>奇趣事</td>
                        <td>funny</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript:void(0)" class="btn btn-danger btn-xs ">删除</a>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="javascript:void(0)" class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                                data-target="#myModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <!-- 引入模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="exampleModalLabel">New message</h4>
            </div>
            <div class="modal-body">
              <form>
                  <!-- 创建一个隐藏域 -->
                <input type="hidden" id="categoryId" name="id">
                <div class="form-group">
                  <label for="recipient-name"  class="control-label">分类名称:</label>
                  <input type="text" name='name' class="form-control" id="recipient-name">
                </div>
                <div class="form-group">
                  <label for="message-text"  class="control-label">分类别名:</label>
                  <input class="form-control" name='slug' id="message-text"></input>
                </div>
              </form>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
              <button type="button" class="btn btn-primary addOrEdit">Send message</button>
            </div>
          </div>
        </div>
      </div>
    </body>
       <!-- 创建模板引擎 -->
    <script id="article_categoryList" type="text/html">
        {{each data v}}
        <tr>
                        <td>{{v.name}}</td>
                        <td>{{v.slug}}</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-toggle="modal"  data-target="#myModal" class=" btn btn-info btn-xs" data-id="{{v.id}}">编辑</a>
                            <a href="javascript:void(0)" class="btn btn-danger btn-xs btn-delete" data-id='{{v.id}}'>删除</a>
                        </td>
        </tr>
        {{/each}}
    </script>
    <script>
    // 发送ajax请求拿到分类管理
    $(function(){
         function getData(){
           
            $.ajax({
               type:'get',
               url:BigNew.category_list,
               success:function(backData){
                   console.log(backData);
                   let res = template('article_categoryList',backData);
                   $('tbody').html(res);
               },
           })

         } 
         getData();

        // 给模态框绑定事件show.bs.modal
        $('#myModal').on('show.bs.modal', function (e) {
              console.log(e.relatedTarget);
            //   e.relatedTarget判断是谁点击的这个事件
              if(e.relatedTarget ===$('#xinzengfenlei')[0]){
                $('#exampleModalLabel').text('新增分类');
                $('.addOrEdit').text('新增').addClass('btn-primary').removeClass('btn-success');
              }else{
                $('#exampleModalLabel').text('编辑分类');
                $('.addOrEdit').text('编辑').addClass('btn-success').removeClass('btn-primary');

                // 点击编辑时获取相对应文本
                $('#recipient-name').val($(e.relatedTarget).parent().prev().prev().text());
                $('#message-text').val($(e.relatedTarget).parent().prev().text());
               // 隐藏域 保存当前点击的编辑按钮对应的类别id.

                $('#categoryId').val($(e.relatedTarget).attr('data-id'));


              }   
        })   

        //给addOrEdit按钮设置点击事件  
        $('.addOrEdit').on('click',function(){

            //  判断
            if($(this).hasClass('btn-primary')){
              
                // 获取文本
                    let articleCategoryName = $('#recipient-name').val().trim(); //文章类别
                    let articleCategorySlug = $('#message-text').val().trim(); //文章类别别名
                       console.log(articleCategoryName,articleCategorySlug);
                    // 判断值是不是为空
                    if (articleCategoryName == "" || articleCategorySlug == "") {
                        alert('新增的文章和文章类别别名不能为空')
                        return;
                    }
                    $.ajax({
                        type: 'post',
                        url: BigNew.category_add,
                        data: {
                            name: articleCategoryName,
                            slug: articleCategorySlug
                        },
                        success:function(backData){
                            console.log(backData);
                            if(backData.code == 201){
                                    alert('新增成功');
                                     //模态框隐藏
                                     $('#myModal').modal('hide');
                                    //  清空文本框的值
                                     //清空输入框
                                     getData();
                                $('#recipient-name').val("");
                                $('#message-text').val("");
                            }
                        }
                    })
            }else{
               //编辑文章类别.
                    //1.获取模态框里面的隐藏域保存的id, 以及2个文本框输入的, 修改后的文本类别和别名. 
                    let categoryId = $('#categoryId').val(); //id
                    let categoryName = $('#recipient-name').val().trim(); //编辑后的 文章类别
                    let categorySlug = $('#message-text').val().trim(); //编辑后的 文章类别别名. 
                    
                    // serialize()和from方法差不多类似    
                   let forms = $('#myModal form').serialize()
                  console.log(forms);
                         // 判断值是不是为空
                 if (categoryName == "" || categorySlug == "") {
                        alert('新增的文章和文章类别别名不能为空')
                        return;
                    }
                 // 发送ajax请求

                 $.ajax({
                     type:'post',
                     url:BigNew.category_edit,
                     data:{
                         id:categoryId,
                         name:categoryName,
                         slug:categorySlug,
                     },
                     success:function(backData){
                       console.log(backData);
                       getData();
                         $('#recipient-name').val("");
                       $('#message-text').val("");
                       //隐藏模态框
                       $('#myModal').modal('hide');
                     }
                 })
            }

        })
        
        //给模态框的取消按钮设置点击事件
        $('#myModal .btn-cancel').on('click', function () {
                $('#myModal form')[0].reset(); //form表单的reset方法,可以把form表单内容设置为默认值
            });

        //给删除按钮用委托事件绑定
        $('tbody').on('click','a.btn-delete',function(){
            if(confirm('你确定要删除吗')){
                //获取id
             let id = $(this).attr('data-id');
             console.log(id);

             //发送请求
             $.ajax({
                 type:'post',
                 url:BigNew.category_delete,
                 data:{
                     id:id,
                 },
                 success:function(backData){
                     console.log('送到哪健康');
                     if(backData.code == 204){
                         alert('删除成功');
                         getData();
                     }
                 }
             })

            }
        })
    })
    </script>

</html>